package com.xt.tea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.tea.entity.domain.Desk;
import com.xt.tea.entity.enums.RequestResultEnum;
import com.xt.tea.entity.exception.GlobalException;
import com.xt.tea.entity.vo.Condition;
import com.xt.tea.mapper.DeskMapper;
import com.xt.tea.service.DeskService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class DeskServiceImpl extends BaseService implements DeskService {
    private final DeskMapper deskMapper;
    @Override
    public IPage<Desk> getDesk(Condition<Desk> condition) {
        if (condition==null){
            throw new GlobalException(RequestResultEnum.CUSTOM_PARAM_CHECK_FAILED);
        }
        Page<Desk> page = Page.of(condition.getPageIndex(), condition.getPageSize());
        LambdaQueryWrapper<Desk> queryWrapper = new LambdaQueryWrapper<>();

        if (condition.getQueryInfo()!=null){
            if (StringUtils.isNotBlank(condition.getQueryInfo().getDeskNo())){
                queryWrapper.eq(Desk::getDeskNo,condition.getQueryInfo().getDeskNo());
            }
            if (condition.getQueryInfo().getDataStatus()!=null){
                queryWrapper.eq(Desk::getDataStatus,condition.getQueryInfo().getDataStatus());
            }
        }
        return deskMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<Desk> getAllDesk() {
        return deskMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public Integer addDesk(Desk deskVo) {
        return insert(deskMapper,deskVo);
    }

    @Override
    public Desk updateDesk(Desk deskVo) {
        Desk desk = selectById(deskMapper, deskVo.getId());
        return update(deskMapper,deskVo,desk);
    }

    @Override
    public Desk updateDeskStatus(Integer id) {
        Desk desk = selectById(deskMapper, id);
        return updateDataStatusById(deskMapper,desk);
    }

    @Override
    public Integer deleteDesk(Integer id) {
        Desk desk = selectById(deskMapper, id);
        return deleteById(deskMapper,desk);
    }
}
